package com.changgou.config.listener;

import com.changgou.config.configuration.RabbitMQConfig;
import com.changgou.config.pojo.DictData;
import com.changgou.config.pojo.DictType;
import com.changgou.config.service.ISysDictService;
import com.changgou.constants.SysConst;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Jiang Yilin
 * @title: SysDataListner
 * @projectName changgou_parent
 * @description: TODO
 * @date 2020/3/618:42
 */
@Component
public class SysDataListner {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    ISysDictService iSysDictService;

    @RabbitListener( queues=RabbitMQConfig.SYS_DATA)
    public void consumeData(String message){
        if(StringUtils.isNotBlank(message)){
            DictData data = iSysDictService.findById(message);
            if(SysConst.STATUS_AVAILABLE.equals(data.getStatus())){
                redisTemplate.opsForHash().put(data.getDictType(),data.getDictValue(),data.getDictLabel());

            }else{
                redisTemplate.opsForHash().delete(data.getDictType(),data.getDictValue());
            }
        }
    }

    @RabbitListener( queues=RabbitMQConfig.SYS_TYPE)
    public void consumeType(String message){
        if(StringUtils.isNotBlank(message)){
            DictType type = iSysDictService.findTypeById(message);
            if(SysConst.STATUS_AVAILABLE.equals(type.getStatus())){
                List<DictData> datas = iSysDictService.findByType(type.getDictType());
                datas.forEach(data->{
                    redisTemplate.opsForHash().put(data.getDictType(),data.getDictValue(),data.getDictLabel());
                });
            }else{
                redisTemplate.opsForHash().delete(type.getDictType());
            }
        }
    }
}
